package com.mybatis.plus.ctl.base;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.IService;
import com.mybatis.plus.ctl.common.utils.constant.PageConstant;
import com.mybatis.plus.ctl.common.utils.result.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.ServletWebRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.mybatis.plus.ctl.common.utils.constant.ResultMsgConsant.*;
import static com.mybatis.plus.ctl.common.utils.constant.ResultMsgConsant.OperatorWays.ADD_MSG;
import static com.mybatis.plus.ctl.common.utils.constant.ResultMsgConsant.OperatorWays.DELETE_MSG;
import static com.mybatis.plus.ctl.common.utils.constant.ResultMsgConsant.OperatorWays.UPDATE_MSG;
import static com.mybatis.plus.ctl.common.utils.result.CommonReturnMsgUtil.getCommonReturnMsg;

/**
 * 基础Controller，将一些共同的增删改查定义为脚手架
 * @author hedonglin
 * @company 个人
 * @email 1048791780@qq.com
 * @date 2018年02月09
 */
public class BaseController<BaseServiceImpl extends IService,T, PK extends Serializable>{
    @Autowired
    protected HttpServletRequest request;

    @Autowired
    protected HttpServletResponse response;

    @Autowired
    protected BaseServiceImpl baseServiceImpl;

    /**
     * 单条记录查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ResponseBody
    public R get(@PathVariable PK id){
        T entity = (T) baseServiceImpl.selectById(id);
        return R.ok().put(Info_Result_Key,entity);
    }

    /**
     * 新增某条记录
     * @param entity
     * @return
     */
    @PostMapping(value = "/add")
    @ResponseBody
    public R add(@RequestBody T entity){
        boolean b = baseServiceImpl.insert(entity);
        return getCommonReturnMsg(b,ADD_MSG);
    }

    /**
     * 修改某条记录
     * @param entity
     * @return
     */
    @PutMapping(value = "/update")
    @ResponseBody
    public R update(@RequestBody T entity){
        boolean b = baseServiceImpl.updateById(entity);
        return getCommonReturnMsg(b,UPDATE_MSG);
    }

    /**
     * 根据单个Id进行删除
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    @ResponseBody
    public R delete(@PathVariable PK id){
        boolean b = baseServiceImpl.deleteById(id);
        return getCommonReturnMsg(b,DELETE_MSG);
    }

    /**
     * 批量删除数据
     * @param ids
     * @return
     */
    @DeleteMapping(value = "/delete")
    @ResponseBody
    public R delete(@RequestBody List<PK> ids){
        boolean b = baseServiceImpl.deleteBatchIds(ids);
        if(b){
            return R.ok(getSuccessResultMsg(DELETE_MSG));
        }else{
            return R.ok(getFailResultMsg(DELETE_MSG));
        }
    }


    /**
     * 查询(非删除状态的)所有数据
     * @return
     */
    @GetMapping(value = "/all")
    @ResponseBody
    public R all(){
        //如果需要排除删除逻辑状态的数据，请根据注释部分重写
        //EntityWrapper<T> wrapper = new EntityWrapper<>();
        //wrapper.and(DELETE_STATUS_PREFIX+DB_DELETE);
        List<T> list = baseServiceImpl.selectList(null);
        return R.ok().put(List_Result_Key,list);
    }

    /**
     * 分页查询，无查询参数，如需要动态查询，则自己重写该方法
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/listByPage")
    @ResponseBody
    public R listByPage(Integer pageNum, Integer pageSize){
        pageNum = pageNum == null? PageConstant.PAGE_NUM:pageNum;
        pageSize = pageSize == null?PageConstant.PAGE_SIZE:pageSize;
        //开启分页
        Page page = new Page(pageNum, pageSize);
        //查询
        page = baseServiceImpl.selectPage(page);
        return R.ok().put(Page_Result_Key,page);
    }


    /**
     * 获取所有请求参数，封装为map对象
     * @return
     */
    public Map<String,Object> getParameterMap(){
        Enumeration<String> enumeration = request.getParameterNames();
        Map<String,Object> parameterMap = new HashMap<String,Object>();
        while (enumeration.hasMoreElements()){
            String key = enumeration.nextElement();
            String value = request.getParameter(key);
            parameterMap.put(key,value);
        }
        return parameterMap;
    }

    /**
     * 设置分页参数
     * @param pageNum
     * @param pageSize
     * @param <T>
     * @return
     */
    protected <T>Page<T> setPage(Integer pageNum,Integer pageSize){
        pageNum = pageNum == null? PageConstant.PAGE_NUM:pageNum;
        pageSize = pageSize == null?PageConstant.PAGE_SIZE:pageSize;
        return new Page<>(pageNum,pageSize);
    }



    /**
     * 将HttpServletRequest和response封装到ServletWebRequest中
     * @return
     */
    public ServletWebRequest getServletWebRequest(){
        return new ServletWebRequest(request,response);
    }
}
